/*
 * Copyright (c) Created by T C L on 2019.7.5.
 */

package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.IsFastClickUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.TimeCardEntity;
import cn.mumzone.pos.ui.dialog.TimesCardAvailableDetailDialogFragment;
import cn.mumzone.pos.ui.dialog.TimesCardUnavailableDetailDialogFragment;
import cn.mumzone.pos.util.GoodsUtil;

public class ADA_MemberTimesCard extends CommonAdapter<TimeCardEntity> {

    private String memberPhone;

    public ADA_MemberTimesCard(Context context, String memberPhone) {
        super(context);
        this.memberPhone = memberPhone;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_member_time_card;
    }

    @Override
    protected void convert(ViewHolder holder, final TimeCardEntity timeCardEntity, int position) {

        if (timeCardEntity.getStatus() == 1) {//已用完
            holder.setBackgroundRes(R.id.root_layout, R.mipmap.ic_member_times_card_use_up);
        } else if (timeCardEntity.getStatus() == 2) {//已过期
            holder.setBackgroundRes(R.id.root_layout, R.mipmap.ic_member_times_card_expire);
        } else if (timeCardEntity.getStatus() == 3) {//已退卡
            holder.setBackgroundRes(R.id.root_layout, R.mipmap.ic_member_times_card_returned);
        } else {//正常
            holder.setBackgroundRes(R.id.root_layout, R.mipmap.ic_member_times_card_normal);
            holder.setVisible(R.id.iv_limit_card, GoodsUtil.isLimitTimeCard(timeCardEntity));
        }

        // 设置次卡信息
        holder.setText(R.id.tv_card_name, StringUtils.get(timeCardEntity.getGoodsName()));
        holder.setText(R.id.tv_card_des, StringUtils.get(timeCardEntity.getServiceName()));
        holder.setText(R.id.tv_card_use_info, String.format("%s/%s次",
                StringUtils.get(timeCardEntity.getResidueTimes()),
                StringUtils.get(timeCardEntity.getTimes())));
        holder.setText(R.id.tv_card_last_time, String.format("有效期至：%s",
                AppDateUtil.getTimeStamp(timeCardEntity.getEndDate(), AppDateUtil.YYYY_MM_DD2)));



        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(IsFastClickUtil.isFastClick(v)){
                    return;
                }
                final Bundle bundle = new Bundle();
                bundle.putString("cardId", timeCardEntity.getId());
                bundle.putSerializable("timeCardEntity", timeCardEntity);
                bundle.putString("memberPhone", memberPhone);
                switch (timeCardEntity.getStatus()) {
                    case 0://正常
                        TimesCardAvailableDetailDialogFragment timesCardAvailableDetailDialogFragment =
                                new TimesCardAvailableDetailDialogFragment();
                        timesCardAvailableDetailDialogFragment.setArguments(bundle);
                        timesCardAvailableDetailDialogFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "timesCardAvailableDetail");
                        break;
                    case 1:
                    case 2:
                    case 3:
                        TimesCardUnavailableDetailDialogFragment timesCardUnavailableDetailDialogFragment =
                                new TimesCardUnavailableDetailDialogFragment();
                        timesCardUnavailableDetailDialogFragment.setArguments(bundle);
                        timesCardUnavailableDetailDialogFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "timesCardUnavailableDetail");
                        break;
                }

            }
        });
    }
}
